package bby.app.fettool;

import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test2 {
    static class MyLog extends Thread{
        boolean nstop =false;
        @Override
        public void run() {
            File file = new File("data0.txt");
            while (nstop){
                System.out.println(file.length());
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }
    //从index读取文件为字符串,c从第start(>0)行开始
    String readLineFrom(File file,int start){
        if(start<1) return "";
        int cnt=0;
        StringBuffer stringBuffer = new StringBuffer();
        try {
            BufferedReader reader= new BufferedReader(new FileReader(file));
            while (cnt<start){
                String line=reader.readLine();
                if (line==null) return stringBuffer.toString();
                else stringBuffer.append(line+"\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return stringBuffer.toString();
        }
    }
    void run(){
        File file = new File("data0.txt");
        long oldlen=0,newlen=0;
        while (true){
            newlen=file.length();
            if(oldlen!=newlen)
                System.out.println(readLineFrom(file,1));
            oldlen=newlen;
            System.out.println(file.length());
            try {
                Thread.currentThread().sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
                return;
            }
        }
    }
    public static void main(String[] args) {
//        Cilent cilent = new Cilent("172.17.104.238",8080);
//        cilent.connect();
//        InetAddress addr = null;
//        try {
//            addr = InetAddress.getLocalHost();
//        } catch (UnknownHostException e) {
//            e.printStackTrace();
//        }
//        System.out.println("Local HostAddress: "+addr.getHostAddress());
//                    String hostname = addr.getHostName();
//                    System.out.println("Local host name: "+hostname);
        System.out.printf("aaaaa");

//        MyLog myLog =new MyLog();
//        myLog.start();

    }}
//       